package com.sharemedia.views
{
	import com.sharemedia.interfaces.IAudioManager;
	import com.sharemedia.managers.AudioPlayer;
	import com.sharemedia.skins.defaultSkin.PromptListSkin;
	import com.sharemedia.utils.SkinUtil;
	import com.sharemedia.vos.TrackPrompt;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayList;
	
	import spark.components.List;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class PromptList extends SkinnableComponent
	{
		// Set default skin
		SkinUtil.setSkin("com.sharemedia.views.PromptList", PromptListSkin);
		
		public function PromptList()
		{
			super();
			
			BindingUtils.bindSetter(updateData, this, ["audioManager", "prompt"]);
		}
		
		[SkinPart]
		public var list:List;
		
		private var _audioManager:IAudioManager; 
		
		[Bindable]
		public function get audioManager():IAudioManager
		{
			return _audioManager;
		}
		
		public function set audioManager(value:IAudioManager):void
		{
			if(_audioManager != value)
			{
				_audioManager = value;
				
				if(list && _audioManager)
					list.dataProvider = _audioManager.prompt;
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			if(instance == list)
			{
				if(_audioManager)
					list.dataProvider = _audioManager.prompt;
				
				list.addEventListener(MouseEvent.CLICK, promptClickHandler);	
			}
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			if(instance == list)
			{
				list.removeEventListener(MouseEvent.CLICK, promptClickHandler);
			}
		}
		
		private function updateData(value:ArrayList):void
		{
			if(list && list.dataProvider != audioManager.prompt)
			{
				list.enabled = true;
				list.dataProvider = audioManager.prompt;
			}
		}
		
		protected function promptClickHandler(event:MouseEvent):void
		{
			audioManager.offer((list.selectedItem as TrackPrompt).uid);
			list.enabled = false;
		}
	}
}